import { drop } from 'lodash';
export function fullDataDeal(matchList) {
  const startRound = matchList[0].round
  var roundMap = {};
  roundMap[startRound] = []
  var curRound = startRound;
  var prevRound = startRound;
  var flag = 0;
  var addtionList = []
  var sansiFujia = {};
  var juesai = {};
  var qibaFujia = {};
  var wuliuFujia = {}

  var fujia8Flag = false
  matchList.forEach(value => {
    if (value.isAddition) {
      const bean3 = {
        //name: value.id,
        name: '附加待定',
        children: []
      }
      addtionList.push(bean3)
    }
    if("1/2附加赛" === value.roundName) {
      fujia8Flag = true
    }
  })
  console.log(JSON.stringify(addtionList))
  var fujia8Index = 1
  var fujia8 = []
  matchList.forEach(value => {
    if (value.round !== curRound) {
      prevRound = curRound
      curRound = value.round
      roundMap[curRound] = [];
      flag = 0;
    }
    if (value.round === startRound) {
      roundMap[curRound].push({
        //name: value.id,
        name: value.gamePosCode1 ? value.gamePosName1 ? value.gamePosName1 : '轮空' : '轮空',
        weight: 0.1,
      })
      roundMap[curRound].push({
        //name: value.id,
        name: value.gamePosCode2 ? value.gamePosName2 ? value.gamePosName2 : '轮空' : '轮空',
        weight: 0.1,
      })
      //console.log(JSON.stringify(roundMap[curRound]))
    } else {

      if ('决3、4' === value.roundName) {
        sansiFujia = value;
      }

      if ("决1、2" === value.roundName) {
        juesai = value;
      }

      if ('决7、8' === value.roundName) {
        qibaFujia = value;
      }

      if ("决5、6" === value.roundName) {
        wuliuFujia = value;
      }

      if("1/4决赛" === value.roundName) {
        fujia8.push(value)
      }

      if (!value.isAddition) {
        const bean = {
          //name: value.id,
          name: value.gamePosName1 ? value.gamePosName1 ? value.gamePosName1 : '待定' : '待定',
          weight: 2.5,
          children: []
        }

        //添加左树
        bean.children.push(roundMap[prevRound][flag])
        flag++

        //添加附加赛
        if (addtionList.length > 0) {
          if (roundMap[prevRound].length == 4) {
            bean.id = 'four' + 1;
          }
          if (fujia8Flag && roundMap[prevRound].length == 8) {
            bean.id = 'eight' + fujia8Index;
            fujia8Index++;
          }
          //addtionList = drop(addtionList, 1)
        }

        //添加右树
        bean.children.push(roundMap[prevRound][flag])
        flag++
        roundMap[curRound].push(bean)

        const bean2 = {
          //name: value.id,
          name: value.gamePosName2 ? value.gamePosName2 ? value.gamePosName2 : '待定' : '待定',
          weight: 2.5,
          children: []
        }


        //添加左树
        bean2.children.push(roundMap[prevRound][flag])
        flag++

        //添加附加赛
        if (addtionList.length > 0) {
          if (roundMap[prevRound].length == 4) {
            bean2.id = 'four' + 2;
            addtionList = drop(addtionList, 1)
          }
          if (fujia8Flag && roundMap[prevRound].length == 8) {
            bean2.id = 'eight' + fujia8Index;
            fujia8Index++
            addtionList = drop(addtionList, 1)
          }
          
          
        }

        //添加右树
        bean2.children.push(roundMap[prevRound][flag])
        flag++
        roundMap[curRound].push(bean2)
        //console.log(JSON.stringify(roundMap[curRound]))
      }

    }
  })
  
  const dataArray = []
  dataArray.push(roundMap[curRound][0])
  dataArray.push({
    name: juesai.winPos ? juesai.winPos === juesai.gamePosCode1 ? '亚军：'+juesai.gamePosName2 : juesai.gamePosName1 : '待定',
    weight: 0.1,
    children: []
  })
  dataArray.push(roundMap[curRound][1])
  const data2 = {
    id: 'rootE',
    name: '',
    children: [{
      id: 'parent',
      name: juesai.winPos ? juesai.winPos === juesai.gamePosCode1 ? '冠军：'+ juesai.gamePosName1 : juesai.gamePosName2 : '待定',
      weight: 2,
      children: dataArray,
    }]
  };
  
  const fujiaData = {}
  fujiaData.three = sansiFujia.winPos ? sansiFujia.winPos === sansiFujia.gamePosCode1 ? sansiFujia.gamePosName1 : sansiFujia.gamePosName2 : '待定'
  fujiaData.four = sansiFujia.winPos ? sansiFujia.winPos === sansiFujia.gamePosCode1 ? sansiFujia.gamePosName2 : sansiFujia.gamePosName1 : '待定'
  fujiaData.five = wuliuFujia.winPos ? wuliuFujia.winPos === wuliuFujia.gamePosCode1 ? wuliuFujia.gamePosName1 : wuliuFujia.gamePosName2 : '待定'
  fujiaData.six = wuliuFujia.winPos ? wuliuFujia.winPos === wuliuFujia.gamePosCode1 ? wuliuFujia.gamePosName2 : wuliuFujia.gamePosName1 : '待定'
  fujiaData.seven = qibaFujia.winPos ? qibaFujia.winPos === qibaFujia.gamePosCode1 ? qibaFujia.gamePosName1 : qibaFujia.gamePosName2 : '待定'
  fujiaData.eight = qibaFujia.winPos ? qibaFujia.winPos === qibaFujia.gamePosCode1 ? qibaFujia.gamePosName2 : qibaFujia.gamePosName1 : '待定'
  fujia8Index = 1;
  fujia8Flag && fujia8.forEach(value => {
    fujiaData['eight'+ fujia8Index] = value.winPos ? value.winPos === value.gamePosCode1 ? value.gamePosName2 : value.gamePosName1 : '待定'
    fujia8Index++
  })
  console.log(fujiaData)
  return { data: data2, fujiaData: fujiaData }
}

export function middleDataDeal(matchList) {
  const startRound = matchList[0].round
  var roundMap = {};
  roundMap[startRound] = []
  var curRound = startRound;
  var prevRound = startRound;
  var flag = 0;

  matchList.forEach(value => {
    if (value.round !== curRound) {
      prevRound = curRound
      curRound = value.round
      roundMap[curRound] = [];
      flag = 0;
    }
    if (value.round === startRound) {

      const treeNode = {
        name: value.winPos ? value.winPos === value.gamePosCode1 ? value.gamePosName1 : value.gamePosName2 : '待定',
        weight: 0.5,
        children: [],
      }
      treeNode.children.push({
        //name: value.id,
        name: value.gamePosCode1 ? value.gamePosName1 ? value.gamePosName1 : '待定' : '待定',
        weight: 0.5,
      })
      treeNode.children.push({
        //name: value.id,
        name: value.gamePosCode2 ? value.gamePosName2 ? value.gamePosName2 : '待定' : '待定',
        weight: 0.5,
      })
      roundMap[curRound].push(treeNode)
    } else {

      const treeNode = {
        //name: value.id,
        name: value.winPos ? value.winPos === value.gamePosCode1 ? value.gamePosName1 : value.gamePosName2 : '待定',
        weight: 0.5,
        children: []
      }

      //添加左树
      treeNode.children.push(roundMap[prevRound][flag])
      flag++

      //添加右树
      treeNode.children.push(roundMap[prevRound][flag])
      flag++
      roundMap[curRound].push(treeNode)

    }
  })

  const data2 = {
    id: 'rootE',
    name: '',
    weight: 0.5,
    children: roundMap[curRound]
  };

  return { data: data2, fujiaData: {} }
}